/**
 * 异步请求获取添加信息
 */
function addInfo() {
    $.ajax({
        type: "GET",
        url: "add",
        timeout: 3000,
        dataType: "json",
        success: function (res) {
            if(res!=null) {
                showAddMenu(res);
            }else {
                alertMsg("操作失败！",null);
            }
        },
        error: function() {
            alertMsg("操作有误！",function() {window.location.reload();});
        }
    });
}

/**
 * 异步请求获取编辑信息
 * @param id 菜单序号
 */
function editMenu(id) {
    $.ajax({
        type: "GET",
        url: "edit",
        data: "menuId=" + id,
        timeout: 3000,
        dataType: "json",
        success: function (res) {
            if(res!=null) {
                showEditMenu(res);
            }else {
                alertMsg("操作失败！",null);
            }
        },
        error: function() {
            alertMsg("操作有误！",function() {window.location.reload();});
        }
    });
}

/**
 * 显示菜单添加信息弹窗
 * @param data 菜单数据
 */
function showAddMenu(data) {
    //拼接父菜单内容
    var parentContent = "<li class='mt5'><span>父菜单:</span>\n" +
        "<select id='menuParent' name=\"parentName\" style='width: 150px'>\n" +
        "<option value=\"0\" selected=\"selected\">【请选择】</option>\n";
    for (var i = 0; i < data.length; i++) {
        parentContent+="<option value='"+data[i].menuId+"' >"+data[i].menuName+"</option>\n";
    }
    parentContent+="</select>\n" +
        "<span class=\"msg\"></span>\n" +
        "</li>\n";
    //拼接表单内容
    var content = "<div class=\"w100\">\n" +
        "<form id=\"addFr\" action=\"/menu/edit\" method=\"post\">\n" +
        "<ul class=\"w95 fl\">\n" +
        "<li><span class=\"msg\"></span></li>\n" +
        "<li class='mt5'><span>菜单名称:</span>\n" +
        "<input type=\"text\" name=\"menuName\" required=\"required\" placeholder=\"请输入菜单名称\" autocomplete=\"off\">\n" +
        "<span class=\"msg\"></span>\n" +
        "</li>\n" +
        "<li class='mt5'><span>菜单URL:</span>\n" +
        "<input type=\"text\" name=\"url\" placeholder=\"请输入菜单URL\" title=\"请输入菜单URL，例如：/user/list\" autocomplete=\"off\">\n" +
        "<span class=\"msg\"></span>\n" +
        "</li>\n" +
        "<li class='mt5'><span>菜单权限码:</span>\n" +
        "<input type=\"text\" name=\"code\" placeholder=\"请输入菜单权限码\" title=\"请输入菜单权限码，例如：user:list\" autocomplete=\"off\">\n" +
        "<span class=\"msg\"></span>\n" +
        "</li>\n"+
        parentContent+
        "<li class='mt5'><span>菜单等级:</span>\n" +
        "<select id='mlevel' name=\"mlevel\" style='width: 150px'>\n" +
        "<option  value=\"0\" selected=\"selected\">【请选择】</option>\n" +
        "<option  value=\"1\" >1</option>\n" +
        "<option  value=\"2\" >2</option>\n" +
        "</select>\n" +
        "<span class=\"msg\"></span>\n" +
        "</li>\n" +
        "</ul>\n" +
        "<div class=\"clear\"></div>\n" +
        "</form>" +
        "</div>";
    $.confirm({
        title: '菜单信息添加',
        content: content,
        confirmButtonClass: 'btn-info',
        animationSpeed: 1000,
        confirm: function () {
            var data = {};
            var formData = $('#addFr').serializeArray();
            $.each(formData, function() {
                data[this.name] = this.value;
            });
            $.ajax({
                type: "POST",
                url: "add",
                data: {
                    "menuId":0,
                    "menuName":data.menuName,
                    "url":data.url,
                    "code":data.code,
                    "parentName":data.parentName,
                    "mlevel":data.mlevel,
                    },
                timeout: 3000,
                dataType: "text",
                success: function (res) {
                    if(res=="success") {
                        alertMsg("添加成功！",function() {window.location.reload();});
                    }else {
                        alertMsg("添加失败！",null);
                    }
                },
                error: function() {
                    alertMsg("操作有误！",null);
                }
            });
            return false;
        }
    });
    //添加事件
    addEvent();
}

/**
 * 显示菜单编辑信息弹窗
 * @param data 菜单数据
 */
function showEditMenu(data) {
    //拼接父菜单内容
    var parentContent = "<li class='mt5'><span>父菜单:</span>\n" +
        "<select id='menuParent' name=\"parentName\" style='width: 150px'>\n" +
        "<option value=\"0\">【请选择】</option>\n";
    for (var i = 0; i < data.menuParents.length; i++) {
        parentContent+="<option value='"+data.menuParents[i].menuId+"' "+((data.parentId == data.menuParents[i].menuId)?"selected='selected'":"")+" >"+data.menuParents[i].menuName+"</option>\n";
    }
    parentContent+="</select>\n" +
        "<span class=\"msg\"></span>\n" +
        "</li>\n";
    //拼接表单内容
    var content = "<div class=\"w100\">\n" +
        "<form id=\"editFr\" action=\"/menu/edit\" method=\"post\">\n" +
        "<ul class=\"w95 fl\">\n" +
        "<li><span class=\"msg\"></span></li>\n" +
        "<li class='mt5'><span>菜单名称:</span>\n" +
        "<input type=\"text\" name=\"menuName\" required=\"required\" value=\""+data.menuName+"\" autocomplete=\"off\">\n" +
        "<span class=\"msg\"></span>\n" +
        "</li>\n" +
        "<li class='mt5'><span>菜单URL:</span>\n" +
        "<input type=\"text\" name=\"url\" value=\""+data.url+"\" autocomplete=\"off\">\n" +
        "<span class=\"msg\"></span>\n" +
        "</li>\n" +
        "<li class='mt5'><span>菜单权限码:</span>\n" +
        "<input type=\"text\" name=\"code\" value=\""+data.code+"\" autocomplete=\"off\">\n" +
        "<span class=\"msg\"></span>\n" +
        "</li>\n"+
        parentContent+
        "<li class='mt5'><span>菜单等级:</span>\n" +
        "<select id='mlevel' name=\"mlevel\" style='width: 150px'>\n" +
        "<option  value=\"0\">【请选择】</option>\n" +
        "<option value=\"1\" "+(data.mlevel==1?"selected='selected'":"")+" >1</option>\n" +
        "<option value=\"2\" "+(data.mlevel==2?"selected='selected'":"")+" >2</option>\n" +
        "</select>\n" +
        "<span class=\"msg\"></span>\n" +
        "</li>\n" +
        "</ul>\n" +
        "<div class=\"clear\"></div>\n" +
        "<div class=\"main_btn\">\n" +
        "<input type=\"hidden\" name=\"menuId\" value=\""+data.menuId+"\">\n" +
        "</div>\n" +
        "</form>" +
        "</div>";
    $.confirm({
        title: '菜单信息修改',
        content: content,
        confirmButtonClass: 'btn-info',
        animationSpeed: 1000,
        confirm: function () {
            var data = {};
            var formData = $('#editFr').serializeArray();
            $.each(formData, function() {
                data[this.name] = this.value;
            });
            $.ajax({
                type: "POST",
                url: "edit",
                data: {
                    "menuId":data.menuId,
                    "menuName":data.menuName,
                    "url":data.url,
                    "code":data.code,
                    "parentName":data.parentName,
                    "mlevel":data.mlevel,
                    },
                timeout: 3000,
                dataType: "text",
                success: function (res) {
                    if(res=="success") {
                        alertMsg("修改成功！",function() {window.location.reload();});
                    }else {
                        alertMsg("修改失败！",null);
                    }
                },
                error: function() {
                    alertMsg("操作有误！",null);
                }
            });
            return false;
        }
    });
    addEvent();
}
/**
 * 父菜单与菜单等级限制
 */
function addEvent() {
    $("#menuParent").on("change",function () {
        $("#mlevel option").detach();
        $("#mlevel").append("<option  value=\"0\" >【请选择】</option>\n"
            +"<option  value=\"2\" selected=\"selected\">2</option>\n");
    });
    $("#mlevel").on("change",function () {
        if($("#mlevel option:selected").val()==1){
            $("#menuParent").html("<option  value=\"0\"  selected=\"selected\">【请选择】</option>\n");
        }
    });
}
